import {Component, ViewEncapsulation} from '@angular/core';
import {GlobalState} from '../../../global.state';
import {UserService} from '../../../services/app.user';
import {AuthenticationHelper} from '../../../app.authentication';
import {Router}       from '@angular/router';
declare var jQuery:any;
@Component({
    selector: 'ba-page-top',
    styleUrls: ['./baPageTop.scss'],
    templateUrl: './baPageTop.html',
    encapsulation: ViewEncapsulation.None,
    providers: [UserService],
})
export class BaPageTop {
    public isScrolled:boolean = false;
    public isMenuCollapsed:boolean = false;
  //  public profileUrl:any = "http://144.214.55.144:3001";
   // public profileUrl:any = "http://localhost:3001";
    public profileUrl:any = 'http://45.79.103.182:5002';
 
    public test:any = 0;
    public lastName:string;
    public firstName:string;
    public profilePicture:any;

    constructor(private _state:GlobalState, private userService:UserService, private router:Router, private authentication:AuthenticationHelper) {
        this._state.subscribe('menu.isCollapsed', (isCollapsed) => {
            this.isMenuCollapsed = isCollapsed;
        });

        //set profile Information

        let user = this.authentication.getUser();
        if (user != 'undefined' && user) {
            if (user.profileImageURL) {
                this.profilePicture = (this.profileUrl + user.profileImageURL.slice(1));
            }
            if (user.firstName) {
                this.firstName = user.firstName;
            }
            if (user.lastName) {
                this.lastName = user.lastName;
            }

        }
        else {
            this.userService.userProfile({}).subscribe(
                data => this.userProfilesucces(data),
                error => this.userProfilefail(error)
            );
        }
    }


    ngOnInit() {
        this.authentication.getuserValueChangeEmitter().subscribe(item => this.setUserInfo());
    }

    userValueChanged(value) {
        this.setUserInfo();
    }

    public  userProfilesucces(data) {
        this.authentication.setUser(data.profile);
        this.setUserInfo();
    }

    public userProfilefail(error) {
    }

    public setUserInfo() {
        let user = this.authentication.getUser();

        if (user.profileImageURL) {
            this.profilePicture = (this.profileUrl + user.profileImageURL.slice(1));

        }
        if (user.firstName) {
            this.firstName = user.firstName;
        }
        if (user.lastName) {
            this.lastName = user.lastName;
        }

    }


    public toggleMenu() {
        this.isMenuCollapsed = !this.isMenuCollapsed;
        this._state.notifyDataChanged('menu.isCollapsed', this.isMenuCollapsed);
    }

    public scrolledChanged(isScrolled) {
      //  this.isScrolled = isScrolled;
    }


    public logoutSession() {
      let seq = {
        sequence: {
          "pageName": 'logout',
          "timeSpent": 0
        }

      };
      this.userService.setUrlSeq(seq).subscribe();

        this.authentication.removeLoggedIn();
        let data = this.authentication.getToken();

        this.userService.userLogout(data).subscribe(
            data => this.logOutSuccess(data),
            error => this.logOutFail(error)
        );
    }

    public logOutSuccess(result) {
        let data = {};
        this.authentication.setUser(data);
        this.router.navigate(['login']);

    }

    public logOutFail(error) {
    }

    public redirectProfile() {
        this.router.navigate(['profile']);
    }
}
